﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHControlLib.CommonHelpers
{


    /// <summary>
    /// 同步缩放窗体上控件的大小和字体
    /// </summary>
    public class ControlResizerHelper
    {
        class ControlPosAndSize
        {
            public float FrmWidth { get; set; }
            public float FrmHeight { get; set; }
            public int Left { get; set; }
            public int Top { get; set; }
            public int Width { get; set; }
            public int Height { get; set; }
            public float FontSize { get; set; }

        }

        private Form _form;
        private Control _control;

        //句柄,大小信息
        private Dictionary<int, ControlPosAndSize> _dic = new Dictionary<int, ControlPosAndSize>();
        public ControlResizerHelper(Form form)
        {
            _form = form;
            _form.Resize += _form_Resize;//绑定窗体大小改变事件

            _form.ControlAdded += form_ControlAdded;  //窗体上新增控件的处理
            _form.ControlRemoved += form_ControlRemoved;

            SnapControlSize(_form);//记录控件和窗体大小
        }
        public ControlResizerHelper(Control Ctrl)
        {
            _control = Ctrl;
            _control.Resize += _Ctrl_Resize;//绑定窗体大小改变事件

            _control.ControlAdded += Ctrl_ControlAdded;  //窗体上新增控件的处理
            _control.ControlRemoved += Ctrl_ControlRemoved;

            CtrlSnapControlSize(_control);//记录控件和窗体大小
        }

        void form_ControlRemoved(object sender, ControlEventArgs e)
        {
            var key = e.Control.Handle.ToInt32();
            _dic.Remove(key);
        }
        void Ctrl_ControlRemoved(object sender, ControlEventArgs e)
        {
            var key = e.Control.Handle.ToInt32();
            _dic.Remove(key);
        }
        //绑定控件添加事件
        private void form_ControlAdded(object sender, ControlEventArgs e)
        {
            var ctl = e.Control;
            var ps = new ControlPosAndSize
            {
                FrmHeight = _form.Height,
                FrmWidth = _form.Width,
                Width = ctl.Width,
                Height = ctl.Height,
                Left = ctl.Left,
                Top = ctl.Top,
                FontSize = ctl.Font.Size
            };
            var key = ctl.Handle.ToInt32();
            _dic[key] = ps;
        }
        private void Ctrl_ControlAdded(object sender, ControlEventArgs e)
        {
            var ctl = e.Control;
            var ps = new ControlPosAndSize
            {
                FrmHeight = _control.Height,
                FrmWidth = _control.Width,
                Width = ctl.Width,
                Height = ctl.Height,
                Left = ctl.Left,
                Top = ctl.Top,
                FontSize = ctl.Font.Size
            };
            var key = ctl.Handle.ToInt32();
            _dic[key] = ps;
        }
        void _form_Resize(object sender, EventArgs e)
        {
            ResizeControl(_form);
        }

        //void _Ctrl_Resize(object sender, EventArgs e)
        //{
        //    ResizeControl(_control);
        //}

        private void ResizeControl(Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                var key = ctl.Handle.ToInt32();
                if (_dic.ContainsKey(key))
                {
                    var ps = _dic[key];
                    var newx = _form.Width / ps.FrmWidth;
                    var newy = _form.Height / ps.FrmHeight;

                    ctl.Top = (int)(ps.Top * newy);
                    ctl.Height = (int)(ps.Height * newy);

                    ctl.Left = (int)(ps.Left * newx);
                    ctl.Width = (int)(ps.Width * newx);

                    ctl.Font = new Font(ctl.Font.Name, ps.FontSize * newy, ctl.Font.Style, ctl.Font.Unit);

                    if (ctl.Controls.Count > 0)
                    {
                        ResizeControl(ctl);
                    }

                }

            }
        }
        void _Ctrl_Resize(object sender, EventArgs e)
        {
            ResizeCtrlControl(_control);
        }

        private void ResizeCtrlControl(Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                var key = ctl.Handle.ToInt32();
                if (_dic.ContainsKey(key))
                {
                    var ps = _dic[key];
                    var newx =_control.Width / ps.FrmWidth;
                    var newy = _control.Height / ps.FrmHeight;

                    ctl.Top = (int)(ps.Top * newy);
                    ctl.Height = (int)(ps.Height * newy);

                    ctl.Left = (int)(ps.Left * newx);
                    ctl.Width = (int)(ps.Width * newx);

                    ctl.Font = new Font(ctl.Font.Name, ps.FontSize * newy, ctl.Font.Style, ctl.Font.Unit);

                    if (ctl.Controls.Count > 0)
                    {
                        ResizeControl(ctl);
                    }

                }

            }
        }


        /// <summary>
        /// 创建控件的大小快照,参数为需要记录大小控件的 容器
        /// </summary>
        private void SnapControlSize(Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                var ps = new ControlPosAndSize()
                {
                    FrmHeight = _form.Height,
                    FrmWidth = _form.Width,
                    Width = ctl.Width,
                    Height = ctl.Height,
                    Left = ctl.Left,
                    Top = ctl.Top,
                    FontSize = ctl.Font.Size
                };

                var key = ctl.Handle.ToInt32();

                _dic[key] = ps;

                //绑定添加事件
                ctl.ControlAdded += form_ControlAdded;
                ctl.ControlRemoved += form_ControlRemoved;

                if (ctl.Controls.Count > 0)
                {
                    SnapControlSize(ctl);
                }

            }

        }

        private void CtrlSnapControlSize(Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                var ps = new ControlPosAndSize()
                {
                    FrmHeight = _control.Height,
                    FrmWidth = _control.Width,
                    Width = ctl.Width,
                    Height = ctl.Height,
                    Left = ctl.Left,
                    Top = ctl.Top,
                    FontSize = ctl.Font.Size
                };

                var key = ctl.Handle.ToInt32();

                _dic[key] = ps;

                //绑定添加事件
                ctl.ControlAdded += form_ControlAdded;
                ctl.ControlRemoved += form_ControlRemoved;

                if (ctl.Controls.Count > 0)
                {
                    SnapControlSize(ctl);
                }

            }

        }



    }



}
